﻿@using KPIS.GERP.MVC.Classes
@using KPIS.GERP.SERVICE.DAL
@using KPIS.GERP.SERVICE.Model
@using Common = KPIS.GERP.MVC.Classes.Common
@model message

@{
    ViewBag.Title = "ข้อความ";
    var approvalTask = (approval_task)ViewBag.ApprovalTask;
}

@using (Html.BeginForm("TakeAction", "Message", FormMethod.Post))
{
<div style="margin: 0 0 0 100px; width:900px">
     
    <h2>@ViewBag.Title</h2>
    
    <div class="detail" >  
        
        @if(Model != null)
        { 
            <table class="tableUnderLineRow" style="width: 100%" > 
                <tr>
                    <td class="captionCell" >
                        หัวข้อข้อความ
                    </td>
                    <td style="width:270px;">
                        @Model.message_title
                    </td> 
                    <td class="captionCell" >
                        วันที่ส่งข้อความ
                    </td>
                    <td style="width:270px;">
                        @(Model.message_date == null ? "-" : Model.message_date.Value.ToString("dd/MM/yyyy"))
                    </td>
                </tr>
                <tr>
                    <td class="captionCell">
                        ผู้ส่งต่อข้อความ
                    </td>
                    <td colspan="3">
                        @(Model.from_hrm_ps_employees != null ? Model.from_hrm_ps_employees.full_name : "-") 
                    </td>  
                </tr>
                <tr>
                    <td class="captionCell">
                        หมายเหตุแนบ
                    </td>
                    <td colspan="3">
                        @Html.Raw(string.IsNullOrWhiteSpace(Model.remarks) ? "-" : Model.remarks.Replace(Environment.NewLine, "<br/>"))
                    </td>
                </tr>  
                <tr>
                    <td class="captionCell">
                        รายละเอียด
                    </td>
                    <td colspan="3">
                        @Html.Raw(string.IsNullOrWhiteSpace(Model.message_descr) ? "-" : Model.message_descr.Replace(Environment.NewLine, "<br/>"))
                    </td>
                </tr> 
            </table>      
        }
        
    </div>
    
    <h2>รายละเอียด</h2>

    <div class="detail">
     
        @if (approvalTask != null && approvalTask.Reference != null)
        {
            var paramPartial = new {RefSeq = approvalTask.Reference.ref_seq};
            if(!string.IsNullOrWhiteSpace(approvalTask.Reference.ref_action_name) && !string.IsNullOrWhiteSpace(approvalTask.Reference.ref_controller_name))
            {
                @Html.Action(approvalTask.Reference.ref_action_name,approvalTask.Reference.ref_controller_name, paramPartial)
            } else if (!string.IsNullOrWhiteSpace(approvalTask.Reference.ref_action_name) )
            {
            
                @Html.Partial(approvalTask.Reference.ref_action_name, new Absence().GetHrmPsAbsence(approvalTask.Reference.ref_seq.ToString(), null,null,null, true,true).FirstOrDefault())
            } else
            {
                @:การอ้างอิงหน้า View ไม่ได้ทำไว้
            }
        }
     
    </div>
    
    <h2>ข้อมูลคำขอ</h2>

    <div class="detail" >
          
        @if (approvalTask != null && Model != null && Model.message_seq > 0)
        {
                @Html.Hidden("m", Model)
                @Html.Hidden("message_seq", Model.message_seq)
                <table class="tableUnderLineRow" width="100%"> 
                    <tr>
                        <td class="captionCell" style="width: 180px;" >
                            หัวข้อคำขอ
                        </td>
                        <td colspan="3" >
                
                            @approvalTask.Title
                        </td>
                    </tr>  
                    <tr> 
                        <td class="captionCell">
                            คำขอถูกสร้างโดย
                        </td>
                        <td>
                            @(approvalTask.RequestEmp != null ? approvalTask.RequestEmp.full_name : "-")
                        </td>
                        <td class="captionCell">
                            หน่วยงาน
                        </td>
                        <td  >
                            @(approvalTask.RequestEmp != null && approvalTask.RequestEmp.hrm_mp_job_sites != null ? approvalTask.RequestEmp.hrm_mp_job_sites.job_site_name : "-")
                        </td>
                    </tr>  
                    <tr>
                        <td class="captionCell">
                            ประเภทการขออนุมัติ
                        </td>
                        <td>
                            @if (approvalTask.RuleApproves != null && approvalTask.RuleApproves.Count > 0)
                            {
                                try
                                {
                                    @Html.Raw(approvalTask.RuleApproves[0].rule_approve_type.rule_approve_type_name)
                                }
                                catch (Exception ex)
                                {
                                    @Html.Raw("-")
                                }
                            }
                            else
                            {
                                @Html.Raw("-")
                            }
                        </td>  
                        <td style="width: 180px;" class="captionCell">
                            รหัสอ้างอิงคำขอ
                        </td>
                        <td>
                
                            @approvalTask.Reference.ref_seq
                        </td> 
                    </tr>
        
                    <tr>
                        <td class="captionCell">
                            สร้างเมื่อ
                        </td>
                        <td>
                            @approvalTask.StartWhen.ToString("d MMMM yyyy")
                        </td>
                        <td class="captionCell">
                            สิ้นสุดเมื่อ
                        </td>
                        <td>
                            @(approvalTask.StopWhen != null ? approvalTask.StopWhen.Value.ToString("d MMMM yyyy") : "-")
                        </td>
                    </tr>    
        
                    <tr>
                        <td class="captionCell cellHead" colspan="4">
                            <h5>ข้อมูลการอนุมัติ</h5>
                        </td> 
                    </tr>     
        
                    <tr >
                        <td class="captionCell" >
                            ใช้กฎการอนุมัติ
                        </td>
                        <td colspan="3">
                            @(string.IsNullOrWhiteSpace(approvalTask.CurrentRuleApproveTypeName)?"-":approvalTask.CurrentRuleApproveTypeName)
                        </td> 
                    </tr>     
        
                    <tr>
                        <td class="captionCell">
                            อยู่ในขั้นตอนที่
                        </td>
                        <td>
                            @(approvalTask.CurrentStep <= approvalTask.CountRuleApprove ? ""+approvalTask.CurrentStep +" จาก "+ @approvalTask.CountRuleApprove : "จบแล้ว" )
                        </td>
                        <td class="captionCell">
                            สถานะงานนี้ในปัจจุบัน
                        </td>
                        <td style="color:red;font-weight: bold">
                            @approvalTask.StatusWord (@(approvalTask.Completed ? "สิ้นสุดแล้ว" : "ดำเนินการอยู่"))
                        </td>
                    </tr>     
        
                    <tr>
                        <td class="captionCell">
                            ประเภทผู้อนุมัติขั้นตอนปัจจุบัน
                        </td>
                        <td>
                            @(string.IsNullOrWhiteSpace(@approvalTask.CurrentRuleApproveApproverName) ? "-" : "ประเภท" + approvalTask.CurrentRuleApproveApproverName)
                        </td>
                        <td class="captionCell">
                            มีผู้อนุมัติ
                        </td>
                        <td>
                            @(approvalTask.CountApproverEmployee == 0 ? "-" : "จำนวน " + @approvalTask.CountApproverEmployee + " คน ")
                        </td>
                    </tr>     

                    <tr>
                        <td class="captionCell">
                            รายชื่อผู้อนุมัติ
                        </td>
                        <td colspan="3">
                            @if (approvalTask.CurrentRuleApprove != null)
                            {
                                int i = 1;
                                @approvalTask.ApproverEmployees.Aggregate("", (current, em) => current + Html.Raw("" + (i++) + ". " + em.full_name))
                            } else
                            {
                                @("-")
                            }
                 
                        </td>
                    </tr>   
                    <tr>
                        <td class="captionCell">
                            โดยมีสิทธิ์ในการ
                        </td>
                        <td colspan="3">
                            @foreach (var actionWord in approvalTask.ActionWord)
                            {
                                @Html.Raw(actionWord + (approvalTask.ActionWord.Last() != actionWord ? ", " : ""))
                            }
                            @if (approvalTask.ActionWord == null || approvalTask.ActionWord.Count == 0 || approvalTask.ActionWord[0] == string.Empty)
                            {
                                @("-")
                            }
                        </td>
                    </tr>       
                     
                    <tr> 
                        <td  class="captionCell">
                            เหตุผลล่าสุด
                        </td>
                        <td colspan="3">
                            <span style="color:red">@(approvalTask.LastActionData != null ? approvalTask.LastActionData.Reasons : "")</span>
                        </td>
                    </tr>           

                    <tr>
                        <td class="captionCell">
                            ประวัติส่งต่อตามสายบังคับบัญชา
                        </td>
                        <td colspan="3" class="captionCell">
                            <div id="btnShowHide" style="cursor: pointer"><strong>แสดง/ซ่อน</strong></div>
                            <div id="paragraph" class="history_block" style="display: none">@Html.Raw(approvalTask.AllHistory().Replace("\r", "<br/>"))</div>  
                        </td> 
                    </tr>        
                </table>
            
                if (!approvalTask.Completed)
                {
                    <table class="tableUnderLineRow" width="100%"> 
                        <tr>
                            <td class="captionCell cellHead" colspan="4">
                                <h5>คำสั่งต่อไป</h5>
                            </td> 
                        </tr>     
        
                        <tr>
                            <td class="captionCell">
                                หัวข้อข้อความ
                            </td>
                            <td colspan="3"> 
                                @(Html.TextBox("MessageTitle", Model.message_title, new {@style = "margin:10px 10px 10px 10px; width:60%"}))
                            </td>
                        </tr>
                        <tr>
                            <td class="captionCell">
                                หมายเหตุ
                            </td>
                            <td colspan="3"> 
                                @Html.TextArea("Reasons", "", 5, 68, new {@style = "margin:10px 10px 10px 10px;"})
                            </td>
                        </tr>  
                    </table>
                }
                @Html.Hidden("ActionStringHdf")
            }
    </div>
</div>

    
    <div class="pnlCommand1">
                        
        @if(approvalTask != null)
        {
            <text>
            @if (approvalTask.IsContainAction("A"))
             {
                 @Html.ActionLink("อนุมัติ", "TakeAction", new {m = Model, actionString = "A", message_seq = Model.message_seq}, new {@id = "btnTakeActionA", @class = "button-link-red", onclick = "return false;"})
             }
            &nbsp;
            @if (approvalTask.IsContainAction("F"))
             {
                 @Html.ActionLink("ส่งต่อ", "TakeAction", new {m = Model, actionString = "F", message_seq = Model.message_seq}, new {@id = "btnTakeActionF", @class = "button-link", onclick = "return false;"})
             }
            &nbsp;
            @if (approvalTask.IsContainAction("N"))
             {
                 @Html.ActionLink("ส่งกลับ", "TakeAction", new {m = Model, actionString = "N", message_seq = Model.message_seq}, new {@id = "btnTakeActionN", @class = "button-link", onclick = "return false;"})
             }
            &nbsp;
            @if (approvalTask.IsContainAction("J"))
             {
                 @Html.ActionLink("ไม่อนุมัติ", "TakeAction", new {m = Model, actionString = "J", message_seq = Model.message_seq}, new {@id = "btnTakeActionJ", @class = "button-link-red", onclick = "return false;"})
             }
            &nbsp;
            @if (approvalTask.IsContainAction("C"))
             {
                 @Html.ActionLink("ยกเลิก", "TakeAction", new {m = Model, actionString = "C", message_seq = Model.message_seq}, new {@id = "btnTakeActionC", @class = "button-link-red", onclick = "return false;"})
             }
            &nbsp;
            </text>
        }
        @Html.ActionLink("กลับไปที่กล่องข้อความเข้า", "Index", null,new {@class="anchor"})
    </div>
}